package com.villaday.idf.activity;

import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.feedback;
import com.villaday.idf.model.feedbackEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
* 建议与反馈
*
* */
public class AdviceActivity extends BaseActivity implements OkHttpUtil.OnDataListener {

    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.et_advise)
    public EditText etAdviceContent;
    @BindView(R.id.et_phone)
    public EditText etPhone;

    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();


    @Override
    protected int getViewResId() {
        return R.layout.activity_advice;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "建议与反馈", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

    }

    @OnClick({R.id.tv_bind})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bind: {
                if (etAdviceContent.getText().toString().trim().length() == 0) {
                    ToastUtil.initToast(this, "请输入反馈或建议的信息");
                } /*else if (etPhone.getText().toString().trim().length() == 0) {
                    ToastUtil.initToast(this, "请输入手机号码");
                } else if (!etPhone.getText().toString().trim().matches(Constant.Strings.RegexMobile)) {
                    ToastUtil.initToast(this, "请输入正确的手机号码");
                }*/ else {
                    loadingDialog = LoadingDialog.newInstance("提交中...");
                    loadingDialog.show(getFragmentManager());
                    Map<String, Object> map = new HashMap<>();
                    OkHttpUtil.postJson(this, Constant.URL.feedback, new feedback(etAdviceContent.getText().toString().trim(), etPhone.getText().toString().trim()), map, this);
                }
                break;
            }
        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.feedback.equals(url)) { //提交意见反馈
                dismissLoading();
                LogeUtil.e("feedback", json);
                feedbackEntity fbe = gson.fromJson(json, feedbackEntity.class);
                if (Constant.Strings.SUC.equals(fbe.getCode())) {
                    ToastUtil.initToast(this, "提交建议成功，管理员正在赶往处理");
                    finish();
                }

            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }


    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }
}
